<template>
  <div class="table-body">
    <el-table v-loading="loading" :data="historyList">
      <el-table-column label="属性名称" align="center" prop="name"/>
      <el-table-column label="属性类型" align="center">
        <template slot-scope="scope">
          {{ optionsList[scope.row.keyType].label }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleList(scope.row)"
            v-hasPermi="['system:alarm:remove']"
          >View History
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="showLine(scope.row)"
            v-if="scope.row.keyType=='switch'"
            v-hasPermi="['system:alarm:remove']"
          >Edit Execution Command
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <historyList :visible.sync="historyVisible" :ruleData="ruleData"
                 :deviceId="deviceDetail?deviceDetail.id:0"></historyList>
    <buttonAddBooln :visible.sync="showButtonVisible" :optionData="optionData" :cateId="deviceDetail.id" typeLink="device"></buttonAddBooln>
  </div>
</template>

<script>
import {listAlarm, delAlarm} from "@/api/system/alarm";
import historyList from "./historyList";
import buttonAddBooln from "../../category/buttonAddBooln";

export default {
  props: ['deviceDetail'],
  name: "tabWarning",
  components: {historyList,buttonAddBooln},
  data() {
    return {
      loading: true,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null,
        data: null,
      },
      optionsList: {
        switch: {
          value: 'switch',
          label: '开关'
        },
        number: {
          value: 'number',
          label: '数字',
        },
        text: {
          value: 'text',
          label: '字符串'
        }
      },
      historyList: [],
      historyVisible: false,
      ruleData: null,
      optionData: null,
      showButtonVisible: false
    }
  },
  created() {
    this.historyList = JSON.parse(this.deviceDetail.categoryDetail.downData)
    console.log('this.historyList:', this.historyList)
    this.getList()
  },
  methods: {
    format(percentage) {
      return percentage === 100 ? 'success' : `${percentage}%`;
    },
    getList() {
      this.loading = true;
      listAlarm(this.queryParams).then(response => {
        this.roleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** delete按钮操作 */
    handleList(row) {
      this.ruleData = JSON.parse(JSON.stringify(row))
      this.historyVisible = true
      // const ids = row.id || this.ids;
      // this.$modal.confirm('是否确认delete警报编号为"' + ids + '"的数据项？').then(function() {
      //   return delAlarm(ids);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("delete成功");
      // }).catch(() => {});
    },
    showLine(row) {
      this.optionData = row
      this.showButtonVisible = true
    },
  }
}
</script>

<style scoped>
.table-body {
  background: #fff;
  padding: 10px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .1);
  border: 1px solid #e6ebf5;
  border-radius: 5px;
}
</style>
